﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeReborn.Classes
{
    public static class Helpers
    {
        /// <summary>
        /// Converts a hex string to a byte array.
        /// </summary>
        /// <param name="strPattern"></param>
        /// <returns></returns>
        public static byte[] HexStringToArray(String strPattern)
        {
            strPattern = strPattern.Replace(" ", "");
            strPattern = strPattern.Replace("?", "F");
            return Enumerable.Range(0, strPattern.Length)
                             .Where(x => x % 2 == 0)
                             .Select(x => Convert.ToByte(strPattern.Substring(x, 2), 16))
                             .ToArray();
        }

        public static string HexStringToMask(String strPattern)
        {
            strPattern = strPattern.Replace(" ", "");
            string mask = "";
            for (int i = 0; i < strPattern.Length; i += 2)
            {
                char c = strPattern[i];
                if (c == '?')
                {
                    mask += '?';
                }
                else
                {
                    mask += 'x';
                }
            }
            return mask;
        }

        public static short HexStringToShort(String strPattern)
        {
            return Convert.ToInt16(strPattern, 16);
        }
    }
}
